🔗
graphql-binding
可以在graphql server
中嵌入grahql API
核心概念是通过为 GraphQL API 创建一个对象,表现出 API 的功能. 这个对象暴露的方法和GraphQL API schema 中定义的查询方法是镜像关系.
Install
1 | yarn add graphql-binding |
API
constructor
1 | constructor(options: BindingOptions): Binding |
Key | Required | Type | Default | Note |
---|---|---|---|---|
schema |
Yes | GraphQLSchema |
- | The executable GraphQL schema for binding |
fragmentReplacements |
No | FragmentReplacements |
{} |
A list of GraphQL fragment definitions, specifying fields that are required for the resolver to function correctly |
before |
No | () => void |
(() => undefined) |
A function that will be executed before a query/mutation is sent to the GraphQL API |
handler |
No | any |
null |
The handler object from JS Proxy |
subscriptionHandler |
No | any |
null |
… |
query&mutation
1 | binding.query.<rootField>: QueryMap<any> // where <rootField> is the name of a field on the Query type in the mapped GraphQL schema |
binding
对象暴露出两个接口 query
,mutate
可以用于执行操作.分别接收三个参数
实例
假设有下面的 schema
1 | type Query { |
1 | binding.query.user({ id: 'abc' }) |
1 | findUser(parent, args, context, info) { |
subscription
1 | binding.subscription.<rootField>(...): AsyncIterator<any> | Promise<AsyncIterator<any>> |
简单实例
1 | const { makeExecutableSchema } = require('graphql-tools') |
与 Prisma 的联系
graphcool-binding(prisma)
是这个版本的实现方案
1 | // Instantiate `Prisma` based on concrete service |
内部机制
prisma 的函数调用最终翻译为 graphql-binding
的方式
1 | prisma.exists.Post({ |